import { AppComponentBase } from "./app-component-base";
import { Injector, OnInit } from '@angular/core';

export class PagedResultDto {
  items: any[];
  totalCount: number;
}

export class EntityDto {
  id: number;
}

export class PagedRequestDto {
  pageIndex: number;
  pageSize: number;
  sortField: string;
  sortOrder:string;
}

export abstract class PagedListingComponentBase<EntityDto> extends AppComponentBase implements OnInit {

  public pageSize: number = 10;
  public pageIndex: number = 1;
  public totalPages: number = 1;
  public totalCount: number;
  public isTableLoading = false;
  public sortField = null; // 排序字段
  public sortOrder = null; // 排序方式顺序倒序

  constructor(injector: Injector) {
    super(injector);
  }

  ngOnInit(): void {
    this.refresh();
  }

  refresh(): void {
    this.getDataPage(this.pageIndex);
  }

  sort(sort: { key: string, value: string }): void {
    this.sortField = sort.key;
    this.sortOrder = sort.value;
    this.refresh();
  }

  public showPaging(ftotalCount: number, fpageIndex: number, ftotalPages: number = null): void {
    if (ftotalPages) {
      this.totalPages = ftotalPages;
    } else {
      this.totalPages = ((ftotalCount - (ftotalCount % this.pageSize)) / this.pageSize) + 1;
    }

    this.totalCount = ftotalCount;
    this.pageIndex = fpageIndex;
  }

  public getDataPage(page: number): void {
    var req = new PagedRequestDto();
    req.pageSize = this.pageSize;
    req.pageIndex = page;

    this.isTableLoading = true;
    this.list(req, () => {
      this.isTableLoading = false;
    });
  }

  protected abstract list(request: PagedRequestDto, finishedCallback: Function): void;
  protected abstract delete(entity: EntityDto): void;
}
